package com.g4.shoppingback.service.impl;

import com.g4.shoppingback.common.PageVo;
import com.g4.shoppingback.exception.ServiceException;
import com.g4.shoppingback.mapper.DdOrderInfoMapper;
import com.g4.shoppingback.mapper.DdSubOrderInfoMapper;
import com.g4.shoppingback.pojo.DdOrderInfo;
import com.g4.shoppingback.pojo.DdSubOrderInfo;
import com.g4.shoppingback.service.DdSubOrderInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class DdSubOrderInfoServiceImpl implements DdSubOrderInfoService {

    @Autowired
    private DdSubOrderInfoMapper ddSubOrderInfoMapper;
    @Autowired
    private DdOrderInfoMapper ddOrderInfoMapper;

    /**
     * 根据分页信息或商品名查询订单
     * @param  query,pageNum,orId 关键字,页数,订单号
     * @return 返回分页信息
     */
    @Override
    public PageVo selectUserByPaged(String query, Integer pageNum, String orId) {
        PageVo pageVo = new PageVo();
        Integer pageSize = 5;   //设置每页固定显示5条数据
        Integer pageStart = (pageNum-1)*pageSize;   //设置从第几条开始
        Long total = ddSubOrderInfoMapper.selectTotal(query);  //查询总条数
        List<DdSubOrderInfo> subOrderInfos = ddSubOrderInfoMapper.selectUserByPaged(query,orId,pageStart,pageSize);
        pageVo.setQuery(query).setPageNum(pageNum).setPageSize(pageSize).setTotal(total).setRows(subOrderInfos);
        return pageVo;
    }

    /**
     * 根据id查询订单明细
     * @param id
     * @return 返回订单明细
     */
    @Override
    public DdSubOrderInfo findById(Integer id) {
        DdSubOrderInfo ddSubOrderInfo = ddSubOrderInfoMapper.selectById(id);
        return ddSubOrderInfo;
    }

    /**
     * 修改商品详情
     * @param ddSubOrderInfo 传入修改信息
     * @return 影响行数
     */
    @Override
    public int updateSubOrder(DdSubOrderInfo ddSubOrderInfo) {
        //获取原总价
        DdSubOrderInfo oldSubOrderInfo = ddSubOrderInfoMapper.selectById(ddSubOrderInfo.getId());
        System.out.println(oldSubOrderInfo);
        BigDecimal oldNum = new BigDecimal(oldSubOrderInfo.getNum());
        BigDecimal oldPrice = oldSubOrderInfo.getActualPrice().multiply(oldNum);
        //获取修改后总价
        BigDecimal num = new BigDecimal(ddSubOrderInfo.getNum());
        BigDecimal newPrice = ddSubOrderInfo.getActualPrice().multiply(num) ;
        BigDecimal totalPrice = ddOrderInfoMapper.selectById(oldSubOrderInfo.getOrId()).getTotalPrice();
        //如果更新后总价减少,那么也更新订单表总价
        if(oldPrice.compareTo(newPrice)==1){
            BigDecimal newTotalPrice = totalPrice.subtract(oldPrice.subtract(newPrice));
            DdOrderInfo ddOrderInfo = new DdOrderInfo().setId(oldSubOrderInfo.getOrId()).setTotalPrice(newTotalPrice);
            int row = ddOrderInfoMapper.updateById(ddOrderInfo);
            if(row==0) throw  new ServiceException("订单价格更新失败");
        }
        //如果更新后总价增加,那么也更新订单表总价
        if(oldPrice.compareTo(newPrice)==-1){
            BigDecimal newTotalPrice = totalPrice.add(newPrice.subtract(oldPrice));
            DdOrderInfo ddOrderInfo = new DdOrderInfo().setId(oldSubOrderInfo.getOrId()).setTotalPrice(newTotalPrice);
            int row = ddOrderInfoMapper.updateById(ddOrderInfo);
            if(row==0) throw  new ServiceException("订单价格更新失败");
        }
        //更新订单表信息
        int row = ddSubOrderInfoMapper.updateById(ddSubOrderInfo);
        return row;
    }

    /**
     * 根据id删除订单
     * @param id 传入订单
     * @return 影响行数
     */
    @Override
    public int deleteById(Integer id) {
        DdSubOrderInfo subOrderInfo = ddSubOrderInfoMapper.selectById(id);
        BigDecimal num = new BigDecimal(subOrderInfo.getNum());
        BigDecimal price = subOrderInfo.getActualPrice().multiply(num);
        BigDecimal totalPrice = ddOrderInfoMapper.selectById(subOrderInfo.getOrId()).getTotalPrice();
        if(totalPrice.compareTo(price)==0){  //如果订单详情总价等于订单总价,那么订单一起删除
            int row = ddSubOrderInfoMapper.deleteById(id);
            if(row==1){
                return ddOrderInfoMapper.deleteById(subOrderInfo.getOrId());
            }return 0;
        }else{ //如果订单详情总价小于订单总价,那么修改订单总价
            int row = ddSubOrderInfoMapper.deleteById(id);
            if(row==1){
                DdOrderInfo ddOrderInfo = new DdOrderInfo().setId(subOrderInfo.getOrId()).setTotalPrice(totalPrice.subtract(price));
                return ddOrderInfoMapper.updateById(ddOrderInfo);
            }return 0;
        }
    }
}
